package com.aqgj.Role.dao;

import com.aqgj.Role.bean.Role;
import com.aqgj.util.BaseDaoImpl;
import com.aqgj.util.Pager;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 
 * </p>
 * 
 * @ClassName RoleDao
 * @JDK 1.5
 * @author 
 * @date  
 */
@Repository("RoleDao")
public class RoleDao extends BaseDaoImpl<Role> {
    public List<Map> selectResourceListByRoleId(String id){
        String sql = "SELECT e.id AS id,s.url AS url FROM" +
                " role r LEFT JOIN role_resource e ON r.id = e.role_id " +
                " LEFT JOIN resource s ON e.resource_id = s.id " +
                " WHERE r.id = ? ";
        return super.currentSession().createNativeQuery(sql).setParameter(1,id).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).getResultList();
    }

    public List<Object[]> selectResourceIdListByRoleId(String id){
        String sql = "SELECT e.resource_id AS id,e.checked FROM role r  LEFT JOIN role_resource e ON r.id = e.role_id  WHERE r.id = ?";
        return super.currentSession().createNativeQuery(sql).setParameter(1,id).getResultList();
    }

    public Pager<Role> getRoles(Integer pageSize, Integer pageNumber){
        Pager<Role> pager = new Pager<>();
        pager.setCurrentPage(pageNumber);
        pager.setPageSize(pageSize);
        String sql ="select * from role limit ?,?";
        String countSql="select count(id) from role";
        pager.setContent(this.getHibernateTemplate().getSessionFactory().getCurrentSession().createNativeQuery(sql,Role.class).setParameter(1,pager.getStart()).setParameter(2,pageSize).getResultList());
        pager.setRecordTotal(((BigInteger)this.createSQLQuery(countSql).getResultList().get(0)).intValue());
        return pager;
    }
}